package com.cskaoyan.user.controller;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.util.CookieUtil;
import com.cskaoyan.mall.commons.util.jwt.JwtTokenUtils;
import com.cskaoyan.user.dal.persistence.MemberMapper;
import com.cskaoyan.user.dto.JwtUserToken;
import com.cskaoyan.user.dto.UserCheckLoginResponse;
import com.cskaoyan.user.dto.UserLoginResponse;
import com.cskaoyan.user.form.KaptchaCodeRequest;
import com.cskaoyan.user.form.UserLoginRequest;
import com.cskaoyan.user.form.UserRegisterRequest;
import com.cskaoyan.user.service.IKaptchaService;
import com.cskaoyan.user.service.ILoginService;
import com.cskaoyan.user.service.IUserRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Auther: Yang Yi-zhou
 * @Date: 2022/9/21 09:37
 * @Description:
 */

@RequestMapping("user")
@RestController
public class UserController {

    @Autowired
    IKaptchaService kaptchaService;
    @Autowired
    MemberMapper memberMapper;
    @Autowired
    IUserRegisterService userRegisterService;
    @Autowired
    ILoginService loginService;


    @PostMapping("register")
    public ResponseData userRegister(@RequestBody UserRegisterRequest userRegisterRequest, HttpServletRequest httpServletRequest) {
        String userName = userRegisterRequest.getUserName();
        String userPwd = userRegisterRequest.getUserPwd();
        String captcha = userRegisterRequest.getCaptcha();
        String email = userRegisterRequest.getEmail();
        // 验证验证码
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        String kaptcha_uuid = CookieUtil.getCookieValue(httpServletRequest, "kaptcha_uuid");
        kaptchaCodeRequest.setUuid(kaptcha_uuid);
        kaptchaCodeRequest.setCode(captcha);
        kaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        // 注册
        userRegisterService.register(userRegisterRequest);

        return new ResponseUtil<>().ok();
    }

    @PostMapping("login")
    public ResponseData userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        // 验证验证码
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        String captcha = userLoginRequest.getCaptcha();
        kaptchaCodeRequest.setCode(captcha);
        String kaptcha_uuid = CookieUtil.getCookieValue(httpServletRequest, "kaptcha_uuid");
        kaptchaCodeRequest.setUuid(kaptcha_uuid);
        kaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        // 登陆
        UserLoginResponse userLoginResponse = loginService.login(userLoginRequest);
        // JWT
        JwtUserToken jwtUserToken = new JwtUserToken();
        jwtUserToken.setUid(userLoginResponse.getId());
        jwtUserToken.setUsername(userLoginResponse.getUsername());
        String token = JwtTokenUtils.builder().msg(JSON.toJSONString(jwtUserToken)).build().creatJwtToken();

        Cookie genCookie = CookieUtil.genCookie("access_token", token, "/", 86400);
        httpServletResponse.addCookie(genCookie);

        userLoginResponse.setToken(token);
        return new ResponseUtil<>().setData(userLoginResponse);
    }

    @GetMapping("login")
    public ResponseData userCheckLogin(HttpServletRequest httpServletRequest) {
        String userInfoToken = httpServletRequest.getHeader("user_info");
        JwtUserToken jwtUserToken = JSON.parseObject(userInfoToken, JwtUserToken.class);
        Long uid = jwtUserToken.getUid();
        String username = jwtUserToken.getUsername();
        UserCheckLoginResponse userCheckLoginResponse = loginService.checkLogin(uid);

        return new ResponseUtil<>().setData(userCheckLoginResponse);
    }

    @GetMapping("loginOut")
    public ResponseData userLoginOut(HttpServletResponse httpServletResponse) {
        Cookie accessToken = CookieUtil.genCookie("access_token", "", "/", 0);
        httpServletResponse.addCookie(accessToken);
        return new ResponseUtil<>().ok();
    }

    @GetMapping("verify")
    public ResponseData userVerify(String uuid, String username) {
        loginService.verify(uuid,username);
        return new ResponseUtil<>().ok();
    }


}
